package com.spring.api.web.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.common.collect.Maps;
import com.spring.api.web.model.DrawJoiner;
import com.spring.api.web.service.DrawApiService;
import com.spring.security.core.annotation.OaUserPermission;
import com.spring.security.web.controller.BaseController;
import com.spring.security.web.model.BaseResult;
import com.wordnik.swagger.annotations.ApiOperation;

/**
 * 抽奖相关接口
 * @author wojunyu
 *
 */
@RestController
@RequestMapping(value="/draw")
public class DrawApiController extends BaseController{
	
	@Resource
	private DrawApiService drawApiService;
	
	
	/**
	 * 场次
	 * @param callback
	 * @return
	 */
	@OaUserPermission(value="/draw/drawEvent")
	@RequestMapping(value="/drawEvent",method=RequestMethod.GET)
	@ApiOperation(value = "获取抽奖场次", httpMethod = "GET", response = Object.class, notes = "获取抽奖场次",produces = "application/json; charset=utf-8")
	public Object drawEvent(String eventId,String callback){
		Map<String,Object> mc = drawApiService.checkDrawID(eventId);
		if(mc!=null){
			return mc;
		}
		List<Map<String,Object>> retO = drawApiService.drawEvent(eventId);
		return buildSuccessResultInfo(retO,"",callback);
	}
	
	/**
	 * 奖项
	 * @param eventId
	 * @param callback
	 * @return
	 */
	@OaUserPermission(value="/draw/drawAwards")
	@RequestMapping(value="/drawAwards",method=RequestMethod.GET)
	@ApiOperation(value = "根据场次编号获取抽奖奖项", httpMethod = "GET", response = Object.class, notes = "根据场次编号获取抽奖奖项",produces = "application/json; charset=utf-8")
	public Object drawAwards(@RequestParam String eventId,String callback){
		Map<String,Object> mc = drawApiService.checkDrawID(eventId);
		if(mc!=null){
			return mc;
		}
		Map<String,Object> parm = Maps.newHashMap();
		parm.put("eventId", eventId);
		List<Map<String,Object>> retO = drawApiService.drawAwards(parm);
		return buildSuccessResultInfo(retO,"",callback);
	}
	
	/**
	 * 根据场次编号获取候选人名单
	 * @param eventId
	 * @return
	 */
	@OaUserPermission(value="/draw/drawJoiner")
	@RequestMapping(value="/drawJoiner",method=RequestMethod.GET)
	@ApiOperation(value = "根据场次编号获取候选人名单", httpMethod = "GET", response = Object.class,
		notes = "根据场次编号获取候选人名单",produces = "application/json; charset=utf-8")
	public Object drawJoiner(@RequestParam String eventId,String callback){
		Map<String,Object> mc = drawApiService.checkDrawID(eventId);
		if(mc!=null){
			return mc;
		}
		Map<String,Object> parm = Maps.newHashMap();
		parm.put("eventId", eventId);
		//根据场次编号获取当前未中奖的候选人信息
		List<DrawJoiner> retO = drawApiService.drawJoiner(parm);
		Set<DrawJoiner> set = new HashSet<DrawJoiner>();
		for(DrawJoiner one:retO){
			set.add(one);
		}
		return buildSuccessResultInfo(set,"",callback);
	}
	
	/**
	 * 根据场次编号或奖项获取中奖人名单
	 * @param eventId
	 * @param callback
	 * @return
	 */
	@OaUserPermission(value="/draw/drawWinner")
	@RequestMapping(value="/drawWinner",method=RequestMethod.GET)
	@ApiOperation(value = "根据场次编号或奖项获取中奖人名单", httpMethod = "GET", response = Object.class, 
		notes = "根据场次编号或奖项获取中奖人名单",produces = "application/json; charset=utf-8")
	public Object drawWinner(@RequestParam String eventId,String awardId,
			String callback){
		Map<String,Object> mc = drawApiService.checkDrawID(eventId);
		if(mc!=null){
			return mc;
		}
		Map<String,Object> parm = Maps.newHashMap();
		parm.put("eventId", eventId);
		parm.put("awardId", awardId);
		//根据场次编号获取当前未中奖的候选人信息
		List<DrawJoiner> retO = drawApiService.drawWinner(parm);
		return buildSuccessResultInfo(retO,"",callback);
	}
	
	/**
	 * 抽奖,返回中奖人
	 * @param eventId
	 * @param awardId
	 * @param callback
	 * @return
	 */
	@OaUserPermission(value="/draw/draw")
	@RequestMapping(value="/draw",method=RequestMethod.GET)
	@ApiOperation(value = "根据场次编号及奖项编号抽奖,返回中奖人", httpMethod = "GET", response = Object.class, 
		notes = "根据场次编号及奖项编号抽奖,返回中奖人",produces = "application/json; charset=utf-8")
	public Object draw(@RequestParam String eventId,@RequestParam String awardId,String wxOpenId,
			String callback){
		Map<String,Object> mc = drawApiService.checkDrawID(eventId);
		if(mc!=null){
			return mc;
		}
		BaseResult retO = null;
		if(StringUtils.isNotBlank(wxOpenId)){
			Map<String,Object> parm = Maps.newHashMap();
			parm.put("eventId", eventId);
			parm.put("awardId", awardId);
			parm.put("wxOpenId", wxOpenId);
			//前台抽奖
			retO = drawApiService.drawByWeb(parm);
		}else{
			Map<String,Object> parm = Maps.newHashMap();
			parm.put("eventId", eventId);
			parm.put("awardId", awardId);
			//系统抽奖
			retO = drawApiService.draw(parm);
		}
		
		if(StringUtils.isNotBlank(callback)){
    		return new JSONPObject(callback, retO);
    	}else{
    		return retO;
    	}
	}
	
	/**
	 * 取消中奖
	 * @param eventId
	 * @param awardId
	 * @param callback
	 * @return
	 */
//	@RequestMapping(value="/removeDraw",method=RequestMethod.GET)
//	@ApiOperation(value = "根据场次编号奖项编号及中奖者微信openid取消中奖", httpMethod = "GET", response = Object.class, 
//		notes = "根据场次编号奖项编号及中奖者微信openid取消中奖",produces = "application/json; charset=utf-8")
//	public Object unDraw(@RequestParam String eventId,@RequestParam String awardId,@RequestParam String wxOpenId,
//			String callback){
//		//根据场次编号获取当前未中奖的候选人信息
//		/*Integer retI = */drawApiService.unDraw(eventId,awardId,wxOpenId);
////		System.out.println(retI+"");
//		return buildSuccessResultInfo(null,"",callback);
//	}
	
	@OaUserPermission(value="/draw/removeDraw")
	@RequestMapping(value="/removeDraw",method=RequestMethod.GET)
	@ApiOperation(value = "根据场次编号奖项编号及中奖者微信openid取消中奖", httpMethod = "GET", response = Object.class, 
		notes = "根据场次编号奖项编号及中奖者微信openid取消中奖",produces = "application/json; charset=utf-8")
	public Object unDraw(@RequestParam String id,
			String callback){
		Map<String,Object> mc = drawApiService.checkDrawID(id);
		if(mc!=null){
			return mc;
		}
		//根据场次编号获取当前未中奖的候选人信息
		/*Integer retI = */drawApiService.unDraw2(id);
//		System.out.println(retI+"");
		return buildSuccessResultInfo(null,"",callback);
	}

}
